import { Object3D, Vector3 } from 'three';
import { Command } from '../command.js';
import { Editor } from '../editor.js';

/**
 * @param editor Editor
 * @param object THREE.Object3D
 * @param newPosition THREE.Vector3
 * @param optionalOldPosition THREE.Vector3
 * @constructor
 */
class SetPositionCommand extends Command {
	object:Object3D;
	oldPosition?:Vector3;
	newPosition?:Vector3;
	constructor( editor:Editor, object:Object3D, newPosition?:Vector3, optionalOldPosition?:Vector3 ) {

		super( editor );

		this.type = 'SetPositionCommand';
		this.name = 'Set Position';
		this.updatable = true;

		this.object = object;

		if ( object !== undefined && newPosition !== undefined ) {

			this.oldPosition = object.position.clone();
			this.newPosition = newPosition.clone();

		}

		if ( optionalOldPosition !== undefined ) {

			this.oldPosition = optionalOldPosition.clone();

		}

	}

	execute() {
		if(this.newPosition)
		this.object.position.copy( this.newPosition );
		this.object.updateMatrixWorld( true );
		this.editor.signals.objectChanged.dispatch( this.object );

	}

	undo() {
		if(this.oldPosition)
		this.object.position.copy( this.oldPosition );
		this.object.updateMatrixWorld( true );
		this.editor.signals.objectChanged.dispatch( this.object );

	}

	update( command:SetPositionCommand ) {
		if(command.newPosition)
		this.newPosition?.copy( command.newPosition );

	}


}

export { SetPositionCommand };
